package vue;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import modele.*;
import modele.listener.*;
import controleur.Controleur;


public class Display implements CouleurLedListener, ManetteListener {
	JFrame frame=new JFrame("Train Aterrissage");
	//JLabel result=new JLabel("Result:");
	//JLabel execution=new JLabel("Execution time:");
	
	private JButton boutonManette=new JButton();
	private JPanel LEDs=new JPanel(new BorderLayout());
	private JLabel imageLEDs= new JLabel();
	
	public JPanelAtterrissage aterrissage;
	public JPanelAtterrissage aterrissage2;
	//public JLabelAtterrissage aterrissageImage2= new JLabelAtterrissage();
	public JPanelAtterrissage aterrissage3;
	//public JLabelAtterrissage aterrissageImage3= new JLabelAtterrissage();
	
	private ImageIcon buttonUp = null;
	private ImageIcon buttonDown = null;
	
	private ImageIcon LEDrouge = null;
	private ImageIcon LEDvert = null;
	private ImageIcon LEDorange = null;
	private ImageIcon LEDeteinte = null;
	
	static ImageIcon ouvert = null;
	static ImageIcon transitionup = null;
	static ImageIcon transitiondown = null;
	static ImageIcon ferme = null;
	
	static ImageIcon porte_ouverte = null;
	static ImageIcon porte_fermee = null;
	static ImageIcon porte_up = null;
	static ImageIcon porte_down = null;
	
	
	
	//long start,stop;
	
	ActionListener controleur;
	
	public void setControleur(final ActionListener controleur){
		this.controleur = controleur;
		boutonManette.addActionListener(controleur);
	}
	
	public Display(){
		
		JPanel panel_gauche = new JPanel();
		panel_gauche.setLayout(new BoxLayout(panel_gauche, BoxLayout.LINE_AXIS));
		JPanel panel_droite = new JPanel();
		panel_droite.setLayout(new BoxLayout(panel_droite, BoxLayout.LINE_AXIS));
		JPanel panel_root = new JPanel();
		panel_root.setLayout(new BoxLayout(panel_root, BoxLayout.PAGE_AXIS));
		
		
		boutonManette.setBorder(BorderFactory.createEmptyBorder());
		boutonManette.setContentAreaFilled(false);
		try {
			buttonUp = new ImageIcon(ImageIO.read(new File("res/images/up.png")));
			buttonDown = new ImageIcon(ImageIO.read(new File("res/images/down.png")));
			LEDrouge = new ImageIcon(ImageIO.read(new File("res/images/rouge.png")));
			LEDvert = new ImageIcon(ImageIO.read(new File("res/images/vert.png")));
			LEDorange = new ImageIcon(ImageIO.read(new File("res/images/orange.png")));
			LEDeteinte = new ImageIcon(ImageIO.read(new File("res/images/eteinte.png")));
			ouvert = new ImageIcon(ImageIO.read(new File("res/images/ouvert.png")));
			ferme = new ImageIcon(ImageIO.read(new File("res/images/ferme.png")));
			transitionup = new ImageIcon(ImageIO.read(new File("res/images/transition.png")));
			transitiondown = new ImageIcon(ImageIO.read(new File("res/images/transitiondown.png")));
			porte_ouverte = new ImageIcon(ImageIO.read(new File("res/images/gears_doors_ouvert.jpg")));
			porte_fermee = new ImageIcon(ImageIO.read(new File("res/images/gears_doors_ferme.jpg")));
			porte_up = new ImageIcon(ImageIO.read(new File("res/images/gears_doors_up.jpg")));
			porte_down = new ImageIcon(ImageIO.read(new File("res/images/gears_doors_down.jpg")));
		} catch (IOException e2) {
			e2.printStackTrace();
		}
		boutonManette = new JButton(buttonUp);
		panel_gauche.add(boutonManette);
		

		imageLEDs = new JLabel("", LEDeteinte, JLabel.CENTER);
		LEDs.add( imageLEDs, BorderLayout.CENTER );
		panel_gauche.add(LEDs);
		
		aterrissage=new JPanelAtterrissage(new BorderLayout(), 0);
		aterrissage2=new JPanelAtterrissage(new BorderLayout(), 1);
		aterrissage3=new JPanelAtterrissage(new BorderLayout(), 2);
		panel_droite.add(aterrissage);
		panel_droite.add(Box.createRigidArea(new Dimension(30, 30)));
		panel_droite.add(aterrissage2);
		panel_droite.add(Box.createRigidArea(new Dimension(30, 30)));
		panel_droite.add(aterrissage3);
		
		
		
		panel_root.add(panel_gauche/*, BorderLayout.LINE_START*/);
		panel_root.add(panel_droite/*, BorderLayout.LINE_END*/);
		frame.setContentPane(panel_root);
		
		
		//boutonManette.addActionListener(controleur);
	

	
		frame.setLayout(new FlowLayout());
		frame.setResizable(false);
		frame.setSize(1090,400);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	public void transitionUp(){
		boutonManette.setIcon(buttonUp);
		
		/*aterrissage.setIcon_transitionup();
		aterrissage2.setIcon_transitionup();
		aterrissage3.setIcon_transitionup();*/
		
	}
	
	public void transitionDown(){
		boutonManette.setIcon(buttonDown);

		/*aterrissage.setIcon_transitiondown();
		aterrissage2.setIcon_transitiondown();
		aterrissage3.setIcon_transitiondown();*/
		
	}
	
	
	
	
	/*public void aterrissageImage_setIcon_ouvert(){
		aterrissage.setIcon_ouvert();
	}
	public void aterrissageImage_setIcon_ferme(){
		aterrissage.setIcon_ferme();
	}

	public void aterrissageImage2_setIcon_ouvert(){
		aterrissage2.setIcon_ouvert();
	}
	public void aterrissageImage2_setIcon_ferme(){
		aterrissage2.setIcon_ferme();
	}

	public void aterrissageImage3_setIcon_ouvert(){
		aterrissage3.setIcon_ouvert();
	}
	public void aterrissageImage3_setIcon_ferme(){
		aterrissage3.setIcon_ferme();
	}*/

	@Override
	public void couleurChanged(CouleurLedChangedEvent event) {
		if(event.getNewCouleur() == LED.Couleur.ETEINT){
			imageLEDs.setIcon(LEDeteinte);
		}
		else if(event.getNewCouleur() == LED.Couleur.ORANGE){
			imageLEDs.setIcon(LEDorange);
		}
		else if(event.getNewCouleur() == LED.Couleur.VERT){
			imageLEDs.setIcon(LEDvert);
		}
	}

	@Override
	public void etatManetteChanged(ManetteChangedEvent event) {
		if(event.getNewEtat() == Manette.Etat.OFF){
			transitionUp();
		}
		else{
			transitionDown();
		}
	}

}




